package com.netease.course.login.web.controller;

import com.netease.course.login.meta.User;
import com.netease.course.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by yanfa on 2016/10/30.
 */
@Controller
public class UserController {

    @Autowired
    UserService userServiceImpl;

    @RequestMapping(value = "/hello")
    public void hello(){
        System.out.println("hello");
    }

    @RequestMapping(value = "/login")
    public String login(//HttpSession session,
                        HttpServletRequest request,
                        HttpServletResponse response,
                        ModelMap modelMap
 ) {
        String userName = (String) request.getParameter("userName");
        String userPassword = (String) request.getParameter("userPassword");
        //cookie session check
        Cookie[] cookies = request.getCookies();
        HttpSession password_session = request.getSession();

        Boolean first_login = true;
        if (cookies != null)
            for (Cookie cookie : cookies)
                if (cookie.getName().equals("userName")) {
                    first_login = false;
                }

        if (first_login == true)
        {
            System.out.println("username:" + userName);
            System.out.println("password:" + userPassword);

            User user = this.userServiceImpl.findOne(userName, userPassword);
            if (user == null)
            {
                return "login_error";
            }
            else
                {
                // 向ModelMap添加内容
                modelMap.addAttribute("userName", user.getUserName());
                Cookie username_cookie = new Cookie("userName", userName);
                password_session.setAttribute("userPassword",userPassword);

                response.addCookie(username_cookie);
                return "login_success";
            }
        }
        else// first_login == false
        {
            //Set Username
            if (cookies != null)
                for (Cookie cookie : cookies)
                    if (cookie.getName().equals("userName"))
                        userName = cookie.getValue();

            //Set password

            userPassword = (String) password_session.getAttribute("userPassword");

            System.out.println(first_login);
            System.out.println("username:" + userName);
            System.out.println("password:" + userPassword);

            User user = this.userServiceImpl.findOne(userName, userPassword);
            if (user == null)
            {
                password_session.invalidate();
                System.out.println("session.invalidate");
                return "login_error";
            }
            else
            {
                // 向ModelMap添加内容
                modelMap.addAttribute("userName", user.getUserName());
                Cookie username_cookie = new Cookie("userName", userName);
                password_session.setAttribute("userPassword",userPassword);

                response.addCookie(username_cookie);
                return "login_success";
            }
        }
    }
}
